from flask import Blueprint, request, jsonify
from models import *
import os

user_bp = Blueprint("user_bp", __name__)

#本地上传图片
@user_bp.route('/upload', methods=['POST'])
def upload():
    file = request.files.get("file")
    filename = file.filename
    path = os.path.join("static/upload/" + filename)
    file.save(path)
    return jsonify({'code': 200, 'url': path})

#相册展示
@user_bp.route('/show_photo',methods=['GET'])
def show_photo():
    list = PhotoModel.query.all()
    all = [{
        'id':i.id,
        'name':i.name
    }for i in list]
    return jsonify({'code':200,'msg':'获取成功','data':all})

#相册添加
@user_bp.route('/add_photo',methods=['POST'])
def add_phtot():
    data = request.json
    if not data:
        return jsonify({'code':400,'msg':'参数错误'})
    name = data.get("name")
    if not all([name]):
        return jsonify({'code':400,'msg':'参数错误'})
    p = PhotoModel(name=name)
    db.session.add(p)
    db.session.commit()
    return jsonify({'code':200,'msg':'添加成功'})

#相册修改
@user_bp.route('/edit_photo/<int:id>',methods=['PUT'])
def edit_photo(id):
    data = request.json
    if not data:
        return jsonify({'code':400,'msg':'参数错误'})
    name = data.get("name")
    info = PhotoModel.query.get(id)
    if not info:
        return jsonify({'code':400,'smg':'要修改的不存在'})
    if name and info.name != name:
        info.name = name
    db.session.commit()
    return jsonify({'code':200,'msg':'修改成功'})

#相册删除
@user_bp.route('/delete_photo/<int:id>',methods=['DELETE'])
def delete_photo(id):
    PhotoModel.query.filter_by(id=id).delete()
    db.session.commit()
    return jsonify({'code':200,'msg':'删除成功'})


#展示图片
@user_bp.route('/show_picture',methods=['GET'])
def show_picture():
    list = request.query.all()
    all = [{
        'id':i.id,
        'name':i.name,
        'url':i.url
    }for i in list]
    return jsonify({'code':200,'msg':'获取成功','data':all})

#添加图片
@user_bp.route('/add_picture',methods=['POST'])
def add_picture():
    data = request.json
    if not data:
        return jsonify({'code':400,'msg':'参数错误'})
    name = data.get("name")
    url = data.get("url")
    if not all([name,url]):
        return jsonify({'code':400,'msg':'参数错误'})
    pi = PictureModel(name=name,url=url)
    db.session.add(pi)
    db.session.commit()
    return jsonify({'code':200,'smg':'添加成功'})

#修改图片
@user_bp.route('/edit_picture/<int:id>',methods=['PUT'])
def edit_picture(id):
    data = request.json
    if not data:
        return jsonify({'code': 400, 'msg': '参数错误'})
    name = data.get("name")
    url = data.get("url")
    info = PictureModel.query.get(id)
    if not info:
        return jsonify({'code':400,'msg':'要修改的不存在'})
    if name and info.name != name:
        info.name = name
    if url and info.url != url:
        info.url = url
    db.session.commit()
    return jsonify({'code':200,'msg':'修改成功'})

#删除图片
@user_bp.route('/delete_picture/<int:id>',methods=['DELETE'])
def delete_picture(id):
    PictureModel.query.filter_by(id=id).delete()
    db.session.commit()
    return jsonify({'code':200,'msg':'删除成功'})